﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Collections;

namespace LedCenter.Models.Program
{
    public class ImageProgram : BaseProgram
    {
        public ushort Width { get; set; }
        public ushort Height { get; set; }
        public ushort BPP { get; set; }

        public string Base64StringData { get; set; }

        public ImageProgram()
            : base(0,PROGRAM_TYPE.IMAGE)
        {
            Width = Height = 1;
            BPP = 2;
            Base64StringData = "";
        }

        public ImageProgram(ushort id,ushort width,ushort height,ushort bpp,string bdata)
            : base(id, PROGRAM_TYPE.IMAGE)
        {
            Width = width;
            Height = height;
            BPP = bpp;
            Base64StringData = bdata;
        }

        #region | IBaseProgram implementations |
        /// <summary>
        /// return xml string as:
        /// <Program Name="Program text" Type="text">
        ///     
        /// </Program>
        /// </summary>
        /// <returns></returns>
        public virtual string ToXML()
        {
            string xmlContent = "<Program></Program>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlContent);
            XmlNode newNode = doc.DocumentElement;
            XmlAttribute nameAttribute = doc.CreateAttribute("Name");
            nameAttribute.Value = ID.ToString();
            newNode.Attributes.Append(nameAttribute);

            XmlAttribute typeAttribute = doc.CreateAttribute("Type");
            typeAttribute.Value = Enum.GetName(typeof(PROGRAM_TYPE), Type);
            newNode.Attributes.Append(typeAttribute);

            System.Diagnostics.Debug.WriteLine(newNode.OuterXml);
            return newNode.OuterXml;
        }

        /// <summary>
        /// Generate bitmap image for program
        /// by default, base program image is solid color
        /// </summary>
        /// <returns></returns>
        public override Bitmap CreateBitmapImage()
        {
            Bitmap bitmapResult = new Bitmap(Width, Height);
            byte[] bitmapData = System.Convert.FromBase64String(Base64StringData);
            BitArray bitMatrix = new BitArray(bitmapData);
            for (int y = 0; y < bitmapResult.Height;y++ )
                for (int x = 0; x < bitmapResult.Width; x++)
                {
                    bitmapResult.SetPixel(x, y, getColor(bitMatrix[y * Width * BPP + x], bitMatrix[y * Width * BPP + x + 1]));
                }
            return bitmapResult;
        }

        private Color getColor(bool rbit,bool gBit)
        {
            if (rbit == false && gBit == false)
                return Color.Transparent;
            else if (rbit == true && gBit == false)
                return Color.Red;
            else if (rbit == false && gBit == true)
                return Color.Green;
            else if (rbit == true && gBit == true)
                return Color.Yellow;

            return Color.Transparent;
        }
        #endregion
    }
}